package com.spring.demo.weixinpay.utils;

import java.util.Random;

/**
 * 获得指定长度的随机字符串
 * @author zjh
 */
public class StringWidthWeightRandom {
    private int length = 32;
    private char[] chars = new char[]{
            '0','1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','V','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
    };
    private Random random = new Random();

    //参数为生成的字符串的长度，根据给定的char集合生成字符串
    public String getNextString(int length){

        char[] data = new char[length];

        for(int i = 0;i < length;i++){
            int index = random.nextInt(chars.length);
            data[i] = chars[index];
        }
        String s = new String(data);
        return s;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
